package app.jobs;

import app.kits.JobEhcacheKit;
import app.models.activity.MallActivity;
import app.models.activity.BrandActivity;
import com.github.sog.annotation.On;
import com.github.sog.libs.AppFunc;
import com.jfinal.plugin.ehcache.CacheKit;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * <p>
 * .活动处理Job
 * </p>
 *
 * @author liuhui
 * @version 1.0 2014-04-29 13:52
 * @since JDK 1.6
 */
@On(name = "activity", value = "0 0/30 * * * ?")
public class ActivityJob implements Job {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String cutTime(String datetime) {
        return datetime.substring(0, datetime.lastIndexOf(":"));
    }
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        if(logger.isDebugEnabled()){
            logger.debug("====================job excute in ======================");
        };
        DateTime now = DateTime.now();
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");

        List mallKeys = CacheKit.getKeys(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME);
        for (Object key : mallKeys) {
            MallActivity activity = CacheKit.get(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,key);
            if(null !=activity.get("end_time")){
                String end_time = cutTime(activity.get("end_time").toString());
                DateTime end = DateTime.parse(end_time, format);
                if(logger.isDebugEnabled()) {
                    logger.debug("=====mallActivity:"+ "~~ end is : " + end);
                }
                //当前时间>活动结束时间，状态设为停止
                if (now.isAfter(end)) {
                    activity.set("state", false);
                    activity.update();
                    CacheKit.remove(JobEhcacheKit.MALL_ACTIVITY_CACHE_NAME,JobEhcacheKit.MALL_ACTIVITY_KEY+activity.get(AppFunc.TABLE_PK_COLUMN));
                    if(logger.isDebugEnabled()) {
                        logger.debug("====update state and remove cachekey is :"+JobEhcacheKit.MALL_ACTIVITY_KEY+activity.get(AppFunc.TABLE_PK_COLUMN) );
                    }
                }
            }
        }

        List brandKeys = CacheKit.getKeys(JobEhcacheKit.BRAND_ACTIVITY_CACHE_NAME);
        for (Object key : brandKeys) {
            BrandActivity activity = CacheKit.get(JobEhcacheKit.BRAND_ACTIVITY_CACHE_NAME,key);
            if(null !=activity.get("end_time")){
                String end_time = cutTime(activity.get("end_time").toString());
                DateTime end = DateTime.parse(end_time, format);
                if(logger.isDebugEnabled()) {
                    logger.debug("=====brandActivity: "+ "~~ end is : "+ end);
                }
                //当前时间>活动结束时间，状态设为停止
                if (now.isAfter(end)) {
                    activity.set("state", false);
                    activity.update();
                    CacheKit.remove(JobEhcacheKit.BRAND_ACTIVITY_CACHE_NAME,JobEhcacheKit.BRAND_ACTIVITY_KEY+activity.get(AppFunc.TABLE_PK_COLUMN));
                    if(logger.isDebugEnabled()) {
                        logger.debug("====update state and remove cachekey is :"+JobEhcacheKit.BRAND_ACTIVITY_KEY+activity.get(AppFunc.TABLE_PK_COLUMN));
                    }
                }
            }
        }
        if(logger.isDebugEnabled()) {
            logger.debug("====================job excute out ======================");
        }

    }
}
